﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Property.Is
{
    /// <summary>
    /// Represents a contract that adheres to <b>property is in</b> semantic.
    /// </summary>
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Property)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class InAttribute : ComparingContractBase
    {
        private IEnumerable _collection;

        /// <summary>
        /// Initializes a new instance of <b>Property.Is.InAttribute</b>.
        /// </summary>
        /// <param name="collection">The collection of items which this property should be a member of.</param>
        /// <exception cref="System.ArgumentNullException">When <i>collection</i> is <b>null</b>.</exception>
        public InAttribute(object[] collection)
        {
            if (collection == null) throw new ArgumentNullException();
            _collection = collection;
        }

        /// <summary>
        /// Performs validation on the property.
        /// </summary>
        /// <param name="property">The property that should be validated.</param>
        /// <returns><b>true</b> if <i>property</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateProperty(object property)
        {
            foreach (var item in _collection)
            {
                if (Comparer.Compare(property, item) == 0) return true;
            }
            return false;
        }
    }
}
